﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace YJKArch.NumberRoom
{
	public partial class PickNumberForm : System.Windows.Forms.Form
	{
		public string RoomTagFirstName
		{
			get
			{
				return this.tbFirstName.Text;
			}
		}

		public int RoomTagNumber
		{
			get
			{
				return Convert.ToInt32(this.tbRoomNumber.Text);
			}
		}

		public string RoomTagSecondName
		{
			get
			{
				return this.tbSecondName.Text;
			}
		}

		public bool IsContinuous
		{
			get
			{
				return this.cmbNumberRule.SelectedIndex == 0;
			}
		}

		public bool PromptSameName
		{
			get
			{
				return this.cmbClashOperate.SelectedIndex == 0;
			}
		}

		public bool CreateRoom
		{
			get
			{
				return this.chbCreateRoom.Checked;
			}
		}

		public PickNumberForm()
		{
			this.InitializeComponent();
			this.tbRoomNumber.Text = "1";
			this.tbFirstName.Text = "房间";
			this.tbPreview.BorderStyle = BorderStyle.None;
			this.UpdatePreview();
			this.cmbNumberRule.SelectedIndex = 0;
			this.cmbClashOperate.SelectedIndex = 0;
		}

		private void UpdatePreview()
		{
			this.tbPreview.Text = this.tbFirstName.Text + this.tbRoomNumber.Text + this.tbSecondName.Text;
		}

		public bool RoomNumberValidating()
		{
			try
			{
				int roomTagNumber = this.RoomTagNumber;
			}
			catch (Exception)
			{
				MessageBox.Show("房间编号需要输入一个整数", "盈建科提示");
				return false;
			}
			return true;
		}

		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		private void PickNumberForm_MouseLeave(object sender, EventArgs e)
		{
			if (!base.ClientRectangle.Contains(base.PointToClient(Cursor.Position)))
			{
				int wMsg = 7;
				PickNumberForm.SendMessage(CmdPickNumberRoom._hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
				return;
			}
			base.Focus();
		}

		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		private void PickNumberForm_FormClosed(object sender, FormClosedEventArgs e)
		{
			int bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			PickNumberForm.keybd_event((byte)bVk, 0, dwFlags, 0);
			PickNumberForm.keybd_event((byte)bVk, 0, dwFlags2, 0);
		}

		protected override void DefWndProc(ref Message m)
		{
			int msg = m.Msg;
			if (msg == 1281)
			{
				this.NumberStep();
				return;
			}
			base.DefWndProc(ref m);
		}

		private void NumberStep()
		{
			int num = Convert.ToInt32(this.tbRoomNumber.Text);
			if (this.IsContinuous)
			{
				num++;
			}
			else
			{
				num += 2;
			}
			this.tbRoomNumber.Text = num.ToString();
			this.UpdatePreview();
		}

		private void tbRoomNumber_TextChanged(object sender, EventArgs e)
		{
			this.UpdatePreview();
		}

		private void tbFirstName_TextChanged(object sender, EventArgs e)
		{
			this.UpdatePreview();
		}

		private void tbSecondName_TextChanged(object sender, EventArgs e)
		{
			this.UpdatePreview();
		}

		private void PickNumberForm_Load(object sender, EventArgs e)
		{
			string path = Path.GetTempPath() + "PickNumberForm.dat";
			if (!File.Exists(path))
			{
				return;
			}
			FileStream fileStream = new FileStream(path, FileMode.Open);
			StreamReader streamReader = new StreamReader(fileStream);
			streamReader.BaseStream.Seek(0L, SeekOrigin.Begin);
			this.tbFirstName.Text = streamReader.ReadLine();
			this.tbSecondName.Text = streamReader.ReadLine();
			int selectedIndex = Convert.ToInt32(streamReader.ReadLine());
			this.cmbNumberRule.SelectedIndex = selectedIndex;
			selectedIndex = Convert.ToInt32(streamReader.ReadLine());
			this.cmbClashOperate.SelectedIndex = selectedIndex;
			bool @checked = Convert.ToBoolean(streamReader.ReadLine());
			this.chbCreateRoom.Checked = @checked;
			streamReader.Close();
			fileStream.Close();
			this.UpdatePreview();
		}

		private void PickNumberForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			FileStream fileStream = new FileStream(Path.GetTempPath() + "PickNumberForm.dat", FileMode.Create);
			StreamWriter streamWriter = new StreamWriter(fileStream);
			streamWriter.WriteLine(this.tbFirstName.Text);
			streamWriter.WriteLine(this.tbSecondName.Text);
			streamWriter.WriteLine(this.cmbNumberRule.SelectedIndex.ToString());
			streamWriter.WriteLine(this.cmbClashOperate.SelectedIndex.ToString());
			streamWriter.WriteLine(this.chbCreateRoom.Checked.ToString());
			streamWriter.Flush();
			streamWriter.Close();
			fileStream.Close();
		}

		public const int USER = 1280;

		public const int WM_MYMESSAGE = 1281;
	}
}
